package org.pictual.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.pictual.business.dao.DAOFactory;
import org.pictual.business.dao.MySQLDAOFactory;
import org.pictual.business.dao.SequenceGeneratorDAO;
import org.pictual.business.dao.exceptions.DAOException;
import org.pictual.business.vo.PalabraVO;


public class PalabraDelegateImpl implements PalabraDelegate{

	private static PalabraDelegateImpl _instance = null;
	protected static Logger log= Logger.getLogger(PalabraDelegateImpl.class);
	protected SequenceGeneratorDAO sequenceGeneratorDAO;
	
	private PalabraDelegateImpl(){}

	public static PalabraDelegateImpl getInstance(){
		if(_instance==null){
    		_instance = new PalabraDelegateImpl();
    	}
        return _instance;
	}

	public PalabraVO readPalabra(int palabra_id) {
		try {
			PalabraVO palabraVO = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getPalabraDAO().readPalabra(palabra_id);
//			MySQLDAOFactory.closeConnection();
			return palabraVO;
		} catch (DAOException e) {
			log.error("readPalabra: DAOException: ",e);
		}
		return null;
	}

	public int insertPalabra(PalabraVO palabraVO) {
		try {
			int id=new Integer(SequenceGeneratorDelegateImpl.getInstance().generateId("palabra"));
			log.info(id);
			palabraVO.setPalabra_id(id);
			DAOFactory.getDAOFactory(DAOFactory.MYSQL).getPalabraDAO().insertPalabra(palabraVO);
//			MySQLDAOFactory.closeConnection();
		} catch (DAOException e) {
			log.error("insertPalabra: DAOException: ",e);
		}
		
		return palabraVO.getPalabra_id();
	}

	public void updatePalabra(PalabraVO palabraVO) {
		try {
			DAOFactory.getDAOFactory(DAOFactory.MYSQL).getPalabraDAO().updatePalabra(palabraVO);
//			MySQLDAOFactory.closeConnection();
		} catch (DAOException e) {
			log.error("updatePalabra: DAOException: ",e);
		}
		
	}
	
	public void setSequenceGeneratorDAO(SequenceGeneratorDAO sequenceGeneratorDAO) {
		this.sequenceGeneratorDAO = sequenceGeneratorDAO;
	}

	@Override
	public boolean haveImages(int palabra_id) {
		try {
			boolean haveImages = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getPalabraDAO().haveImages(palabra_id);
//			MySQLDAOFactory.closeConnection();
			return haveImages;
		
		} catch (DAOException e) {
			log.error("haveImages: DAOException: ",e);
		}
		return false;
		
	}

	@Override
	public List<Integer> withoutImages() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
